{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb812cd2",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "\n",
    "> * 1.list的一般方法/函数的使用案例，如：\n",
    ">> 1. append()增加，增加list元素到list的末尾，常见于新建列表\n",
    ">> 2. pop(index)/remove(values) 对比两者优势\n",
    ">> 3. count()  查看某个元素出现的次数\n",
    ">> 4. index() 查看某个元素第一次出现的位置\n",
    ">> 5. extend() 两个list合并在一起，也可以用[]+[]来·实现\n",
    ">> 6. sort() liat元素的排列\n",
    "\n",
    "> * 2. list的高级用法：如\n",
    ">> 1. copy()list (可变的数据类型) 复制 不等价于 赋值，可用[:]\n",
    "来实现\n",
    ">> 2. enumerate（）枚举，可同时遍历并获取index和values\n",
    "\n",
    "## 本周内容\n",
    "\n",
    "### list练习，vowels.py vowels2.py\n",
    "\n",
    "### list实践 ：\n",
    "\n",
    "### 初始字典："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fbef4ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "> * vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132c98b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels  = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "# 检查 word中出现的元音，并打印出来怎么做？\n",
    "# for letter in word:  #  第一个元素letter 请不要和已有的变量重名\n",
    "                       #  第一个元素一般不定义名字，常用item（元素）来命名\n",
    "                       #                            简写 i 来命名\n",
    "                       #  第二个元素word （重要），意味着将遍历的内容\n",
    "                       #  遍历的内容一般是 序列数据 str list\n",
    "for letter in word:\n",
    "    print(letter)\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5c0e76d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用append（）新建列表\n",
    "found[]\n",
    "for letter in word:\n",
    "    print(letter)\n",
    "    if letter in vowels: # letter 在元音字母表中\n",
    "        if letter not in found: #"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec4546b2",
   "metadata": {},
   "source": [
    "## list实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca4df1e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data = [\"haixin\",\"karry\"]\n",
    "user_balance = [\"300\",\"333\"]\n",
    "goods_list = [\"apple\",\"banana\",\"cookie\",\"milk tea\"]\n",
    "goods_code_list = [\"1\",\"2\",\"3\",\"4\"]\n",
    "goods_price = [\"20\",\"15\",\"50\",\"30\"]\n",
    "shopping_cart = []\n",
    " \n",
    "while True:\n",
    "    remaining_sum = input(\"请输入你的支付宝余额\")\n",
    "    print(goods_list)\n",
    "    user_want = input(\"请输入你想购买商品的编号\")\n",
    "    if user_want in goods_code_list:\n",
    "        if remaining_sum - goods_price > 0:\n",
    "            print(\"购买成功，用户余额为\",\"remaining_sum - goods_price\")\n",
    "        else:\n",
    "            print(\"用户余额不足，您要使用花呗吗？\")\n",
    "            quit = input(\"如果想要推出的话，请按q\")\n",
    "            shopping_cart.append()\n",
    "            print(shopping_cart)\n",
    "            break\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "250a09ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝的余额：5555\n",
      "------------- list --------------\n",
      "商品编号: 0,商品名称：python课本，商品价格：60 元\n",
      "商品编号: 1,商品名称：华为手机，商品价格：8000 元\n",
      "商品编号: 2,商品名称：小米手环，商品价格：199 元\n",
      "商品编号: 3,商品名称：蜜雪冰城，商品价格：8 元\n",
      "请输入商品编号:2\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-b3628e413b60>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     15\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mchoice\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misdigit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgoods_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m             \u001b[1;32mif\u001b[0m \u001b[0mAlipay\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     18\u001b[0m                 \u001b[0mAlipay\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mAlipay\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m                 \u001b[0muser_shopping_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "goods_list = [\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",199],\n",
    "    [\"蜜雪冰城\",8]\n",
    "]\n",
    "user_shopping_list = []\n",
    "\n",
    "Alipay = int(input(\"请输入支付宝的余额：\"))\n",
    "while True:\n",
    "    print('------------- list --------------')\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号: %s,商品名称：%s，商品价格：%s 元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "    choice = input(\"请输入商品编号:\")\n",
    "    if choice.isdigit():\n",
    "        choice = int(choice)\n",
    "        if choice > len(goods_list):\n",
    "            print(\"您选择的商品不存在\")\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                user_shopping_list.append(goods_list[choice])\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "        else:\n",
    "            print(\"请输入正确的商品编号，您输入的商品不存在\")\n",
    "    elif choice == \"q\":\n",
    "        print(\"用户余额：%s,购物车:%s\"%(Alipay,user_shopping_list))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的商品编号，您输入的商品不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65459c37",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
